package com.yds.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;


public class YDSRectAngleImageView extends androidx.appcompat.widget.AppCompatImageView {
    private Paint paint;
    private int rectAngleWidth = 5;
    private int rectAngleHeight = 5;
    private boolean rectAngleLeftTop = false;
    private boolean rectAngleRightTop = false;
    private boolean rectAngleLeftBottom = false;
    private boolean rectAngleRightBottom = false;
    private Paint paint2;

    public YDSRectAngleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public YDSRectAngleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public YDSRectAngleImageView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.YDSRectAngleImageView);
            rectAngleWidth = a.getDimensionPixelSize(R.styleable.YDSRectAngleImageView_yds_rectAngleWidth, rectAngleWidth);
            rectAngleHeight = a.getDimensionPixelSize(R.styleable.YDSRectAngleImageView_yds_rectAngleHeight, rectAngleHeight);
            rectAngleLeftTop = a.getBoolean(R.styleable.YDSRectAngleImageView_yds_rectAngleLeftTop, rectAngleLeftTop);
            rectAngleRightTop = a.getBoolean(R.styleable.YDSRectAngleImageView_yds_rectAngleRightTop, rectAngleRightTop);
            rectAngleLeftBottom = a.getBoolean(R.styleable.YDSRectAngleImageView_yds_rectAngleLeftBottom, rectAngleLeftBottom);
            rectAngleRightBottom = a.getBoolean(R.styleable.YDSRectAngleImageView_yds_rectAngleRightBottom, rectAngleRightBottom);
        } else {
            float density = context.getResources().getDisplayMetrics().density;
            rectAngleWidth = (int) (rectAngleWidth * density);
            rectAngleHeight = (int) (rectAngleHeight * density);
        }

        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        paint2 = new Paint();
        paint2.setXfermode(null);
    }

    @Override
    public void draw(Canvas canvas) {
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(bitmap);
        super.draw(canvas2);
        drawLiftUp(canvas2);
        drawRightUp(canvas2);
        drawLiftDown(canvas2);
        drawRightDown(canvas2);
        canvas.drawBitmap(bitmap, 0, 0, paint2);
        bitmap.recycle();
    }

    /*左上角*/
    private void drawLiftUp(Canvas canvas) {
        if (rectAngleLeftTop) {
            Path path = new Path();
            path.moveTo(0, rectAngleHeight);
            path.lineTo(0, 0);
            path.lineTo(rectAngleWidth, 0);
            path.arcTo(new RectF(
                            0,
                            0,
                            rectAngleWidth * 2,
                            rectAngleHeight * 2),
                    -90,
                    -90);
            path.close();
            canvas.drawPath(path, paint);
        }
    }

    /*左下角*/
    private void drawLiftDown(Canvas canvas) {
        if (rectAngleLeftBottom) {
            Path path = new Path();
            path.moveTo(0, getHeight() - rectAngleHeight);
            path.lineTo(0, getHeight());
            path.lineTo(rectAngleWidth, getHeight());
            path.arcTo(new RectF(0, getHeight() - rectAngleHeight * 2, 0 + rectAngleWidth * 2, getHeight()), 90, 90);
            path.close();
            canvas.drawPath(path, paint);
        }
    }

    /*右上角*/
    private void drawRightUp(Canvas canvas) {
        if (rectAngleRightTop) {
            Path path = new Path();
            path.moveTo(getWidth(), rectAngleHeight);
            path.lineTo(getWidth(), 0);
            path.lineTo(getWidth() - rectAngleWidth, 0);
            path.arcTo(new RectF(getWidth() - rectAngleWidth * 2, 0, getWidth(), 0 + rectAngleHeight * 2), -90, 90);
            path.close();
            canvas.drawPath(path, paint);
        }
    }

    /*右下角*/
    private void drawRightDown(Canvas canvas) {
        if (rectAngleRightBottom) {
            Path path = new Path();
            path.moveTo(getWidth() - rectAngleWidth, getHeight());
            path.lineTo(getWidth(), getHeight());
            path.lineTo(getWidth(), getHeight() - rectAngleHeight);
            path.arcTo(new RectF(getWidth() - rectAngleWidth * 2, getHeight() - rectAngleHeight * 2, getWidth(), getHeight()), 0, 90);
            path.close();
            canvas.drawPath(path, paint);
        }
    }
}
